package com.wsk.util;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * Created by wsk1103 on 2017/9/15.
 */
public class VerifyCodeUtil {
    //验证码类型只为数字，0-9
    public static final int TYPE_NUM_ONLY = 0;
    //。。。字母，A-Za-z
    public static final int TYPE_LETTER_ONLY = 1;
    //数字和大小写字母
    public static final int TYPE_ALL_MIXED = 2;
    //数字和大写字母
    public static final int TYPE_NUM_UPPER = 3;
    //数字和小写字母
    public static final int TYPE_NUM_LOWER = 4;
    //大写字母
    public static final int TYPE_UPPER_ONLY = 5;
    //小写字母
    public static final int TYPE_LOWER_ONLY = 6;

    private VerifyCodeUtil() {
    }

    //生成随机颜色
    private static Color generateRandomColor() {
        Random random = new Random();
        return new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255));
    }

    ///生成图片验证码
    /*
    type:验证码类型，本类的静态属性
    length：验证码字符长度，必须大于0的整数
    excludeString：需要排除的特殊字符
    width：图片宽度（适当大小，4个字符的可以使用85-90）
    height：图片高度
    interLine：图片中干扰线的数量
    randomLocation：每个字符的高低位置是否随机
    backColor：图片颜色：null表示使用随机颜色
    foreColor：字体颜色，null表示采用随机颜色
    lineColor：干扰线颜色，null表示颜色随机
    return 图片缓存对象
     */
    public static BufferedImage generateImageCode(int type, int length, String excludeString, int width, int height, int interLine, boolean randomLocation, Color backColor, Color foreColor, Color lineColor) {
        String textCode = generateTextCode(type, length, excludeString);
        return generateImageCode(textCode, width, height, interLine, randomLocation, backColor, foreColor, lineColor);
    }
    /*
    已有验证码，生成验证码图片
    textCode：文本验证码
    width：图片宽度
    height：图片高度
    interLine：干扰线的条数
    randomLocation：每个字符的高低位置是否随机
    backColor：图片颜色，null表示随机颜色
    foreColor：字体颜色，null表示随机颜色
    lineColor：干扰线颜色，null表示颜色随机
    return 图片缓存对象
     */
    public static BufferedImage generateImageCode(String textCode, int width, int height, int interLine, boolean randomLocation, Color backColor, Color foreColor, Color lineColor) {
        //创建内存图片
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        //获得图像上下文
        Graphics graphics = bufferedImage.getGraphics();
        //画背景图
        graphics.setColor(null == backColor ? generateRandomColor() : backColor);
        graphics.fillRect(0, 0, width, height);
        //画干扰线
        Random random = new Random();
        if (interLine > 0) {
            int x = 0,y = 0 ,x1 = width,y1 = 0;
            for (int i = 0 ;i < interLine; i++) {
                graphics.setColor(null == lineColor ? generateRandomColor() : lineColor);
                y = random.nextInt(height);
                y1 = random.nextInt(height);
                graphics.drawLine(x, y, x1, y1);
            }
        }
        //字体大小为图片高度的80%
        int fsize = (int) (height * 0.8);
        int fx = height - fsize;
        int fy = fsize;
        //设定字体
        graphics.setFont(new Font("Defalut", Font.PLAIN, fsize));
        //写验证码字符
        for (int i = 0;i<textCode.length();i++) {
            fy = randomLocation ? (int) ((Math.random() * 0.3 + 0.6) * height) : fy;
            graphics.setColor(null == foreColor ? generateRandomColor() : foreColor);
            //将验证码显示到图像中
            graphics.drawString(textCode.charAt(i) + "", fx, fy);
            fx += fsize*0.9;
        }
        graphics.dispose();
        return bufferedImage;
    }

    //生成验证码字符串
    /*
    type:同上
    length：验证码长度，必须大于0的整数
    excludeString：需要排除的特殊字符（无需要排除就位null）
    return 验证码字符串
     */
    public static String generateTextCode(int type, int length, String excludeString) {
        if (length < 0) {
            return "";
        }
        StringBuffer verifyCode = new StringBuffer();
        int i = 0;
        Random random = new Random();
        switch (type) {
            case TYPE_NUM_ONLY:
                while (i < length) {
                    int t = random.nextInt(10);
                    if (null == excludeString || !excludeString.contains(t + "")) {
                        verifyCode.append(t);
                        i++;
                    }
                }
                break;
            case TYPE_LETTER_ONLY:
                while (i < length) {
                    int t = random.nextInt(123);
                    if ((t >= 97 || (t >= 65 && t <= 90)) && (null == excludeString || !excludeString.contains(t + ""))) {
                        verifyCode.append((char) t);
                        i++;
                    }
                }
                break;
            case TYPE_ALL_MIXED:
                while (i < length) {
                    int t = random.nextInt(123);
                    if ((t >= 97 || (t >= 65 && t <= 90) || (t >= 48 && t < 57)) && (null == excludeString || !excludeString.contains(t + ""))) {
                        verifyCode.append((char) t);
                        i++;
                    }
                }
                break;
            case TYPE_NUM_UPPER:
                while (i < length) {
                    int t = random.nextInt(123);
                    if ((t >= 65 || (t >= 48 && t <= 57)) && (null == excludeString) || !excludeString.contains(t + "")) {
                        verifyCode.append((char) t);
                        i++;
                    }
                }
                break;
            case TYPE_NUM_LOWER:
                while (i < length) {
                    int t = random.nextInt(123);
                    if ((t >= 97 || (t >= 48 && t <= 57)) && (null == excludeString) || !excludeString.contains(t + "")) {
                        verifyCode.append((char) t);
                        i++;
                    }
                }
                break;
            case TYPE_UPPER_ONLY:
                while (i < length) {
                    int t = random.nextInt(91);
                    if ((t >= 65 ) && (null == excludeString) || !excludeString.contains(t + "")) {
                        verifyCode.append((char) t);
                        i++;
                    }
                }
                break;
            case TYPE_LOWER_ONLY:
                while (i < length) {
                    int t = random.nextInt(123);
                    if ((t >= 97) && (null == excludeString) || !excludeString.contains(t + "")) {
                        verifyCode.append((char) t);
                        i++;
                    }
                }
                break;
        }
        return verifyCode.toString();
    }
}
